package com.wxxymaker.utils.web;

import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @Author: Soft
 * @Date: 2018/5/11 1:23
 * @Desc: 阶段打分统计工具
 */
public class StageCountUtil {

    public static CountResult countScore(long... gradeNumber) {
        if (gradeNumber.length == 4) {
            return StageCountUtil.countScore(gradeNumber[0], gradeNumber[1], gradeNumber[2], gradeNumber[3]);
        }
        return new CountResult(0.0, true);
    }

    /**
     * 给阶段中一个公司的不同等级的人数计算出总分
     * @param one   差评人数
     * @param two   及格人数
     * @param three 良好人数
     * @param four  优秀人数
     * @return 总评分数 是否建议作废（建议作废没办法从主观上改变参评人员评价分数的随意性！！！！）
     */
    public static CountResult countScore(long one, long two, long three, long four) {
        try {
            double[] standards = getStandard();
            double one_score = standards[0], //差评等级转百分制权值
                    two_score = standards[1],
                    three_score = standards[2],
                    four_score = standards[3];
            long all = one + two + three + four;//参评总人数
            if (all <= 0) {
                return new CountResult(0.0, true);
            }
            double total = (one_score * one + two_score * two + three_score * three + four_score * four) / all;//四星为满分的分数
            //权值计算总分，标准科学，但是不够折线图直观，折线图可依据不同管理员的不同的看法进行打分比较！
            double sum = one * Math.pow((one_score - total), 2) + two * Math.pow((two_score - total), 2) + three *
                    Math.pow((three_score - total), 2) + four * Math.pow((four_score - total), 2);
            double s = Math.sqrt(sum / all);//方差
            double cv = s / total;//差异系数
            boolean limit;
            if ((one != 0) && (four != 0)) {
                limit = ((one + four) >= 0.1 * all) && ((one > four) ? ((one / four) <= 1.2) : ((four / one) <= 1.2));
            } else {
                limit = false;
            }

            //0.1根据情况可变
            return new CountResult(25 * total, (cv >= 0.2) && limit);
        } catch (Exception e) {
            return new CountResult(0.0, true);
        }
    }

    /**
     * 计算后返回的结果
     * 包括计算后的总分和建议（是否作废）
     */
    public static class CountResult {
        private double total;  // 总评分数
        private boolean cancel; // 是否作废

        public CountResult(double total, boolean cancel) {
            this.total = total;
            this.cancel = cancel;
        }

        public double getTotal() {
            return total;
        }

        public void setTotal(double total) {
            this.total = total;
        }

        public boolean isCancel() {
            return cancel;
        }

        public void setCancel(boolean cancel) {
            this.cancel = cancel;
        }
    }

    /**
     * 从配置文件中获取评分标准
     * @return 标准数组
     */
    public static double[] getStandard() {
        FileInputStream in = null;
        try {
            File file = ResourceUtils.getFile("classpath:standard.properties");
            in = new FileInputStream(file);

            Properties ps = new Properties();
            ps.load(in);
            Double one = Double.valueOf(ps.getProperty("one")) / 25;
            Double twe = Double.valueOf(ps.getProperty("twe")) / 25;
            Double three = Double.valueOf(ps.getProperty("three")) / 25;
            Double four = Double.valueOf(ps.getProperty("four")) / 25;
            return new double[]{one, twe, three, four};
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return new double[]{2, 2.4, 3.4, 4};
    }
}
